﻿function deleteTask(mySelector) {
    var answer = confirm("Etes vous sur ?");
    if (answer)
        $.ajax({
            url: "/Task/Delete",
            type: 'DELETE',
            dataType: 'json',
            data: {
                'Id': mySelector.parent().parent().attr('id').split("-")[1]
            },
            success: function (myData) {
                mySelector.parent().parent().remove();
            },
            error: function (jqXHR, textStatus, errorThrown) {

            }
        });
}


function createTask() {
    $.ajax({
        url: "/Task/Create",
        type: 'POST',
        dataType: 'json',
        data: {
            'Title': $('#taskAddTitle').val(),
            'Description': $('#taskAddDescription').val(),
            'PriorityLabel': $('#taskAddPriority').val(),
            'Progression': $('#taskAddProgression').val(),
            'UserName': $('#taskAddUserName').val()
        },
        success: function (myData) {
            TaskRefresh();
            $('#osx-modal-content').dialog('close');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Une erreur s'est produite lors de la création de la tâche !");
        }
    });
}


// APPELLEE PLUSIEURS FOIS.
function TaskRefresh() {
    $.ajax({
        url: "/Task", // A faire
        type: 'GET',
        dataType: 'json',
        data: {},
        success: function (myData) {
            $(".tb").hide();
            $(".tb").children().remove();

            //Génération du tableau de taches
            for (var i = 0; i < myData.length; i++) {
                var myTaskLine = $('<tr id="task-' + myData[i].Id + '">');
                myTaskLine.append('<td>' + myData[i].Id + '</td>');
                myTaskLine.append('<td class="title">' + myData[i].Title + '</td>');
                myTaskLine.append('<td class="description">' + myData[i].Description + '</td>');
                myTaskLine.append('<td class="priorityvalue">' + myData[i].Priority + '</td>');
                myTaskLine.append('<td class="progression">' + myData[i].Progression + '</td>');
                myTaskLine.append('<td class="assigne">' + myData[i].Assignee.UserName + '</td>');
                myTaskLine.append('<td class="popup"><a href="#" class="popup"><img src ="/Content/images/edit.png" /></a></td>');
                myTaskLine.append('<td class="delb"><a href="#"><img src ="/Content/images/delete.png" /></a></td>');
                $('tbody.tb').append(myTaskLine);

                $('tr#task-' + myData[i].Id + ' > td.popup > a').click(function () {
                    $('#taskIdHidden').val($(this).parent().parent().attr('id').split('-')[1]);
                    $('#dialog').dialog('open');
                    var tst = $('tr#task-' + $('#taskIdHidden').val() + ' > td.resume');
                    $('input#editTaskTitle').val($('tr#task-' + $('#taskIdHidden').val() + ' > td.title').text());
                    $('input#editTaskDescription').val($('tr#task-' + $('#taskIdHidden').val() + ' > td.description').text());
                    $('input#editTaskPriority').val($('tr#task-' + $('#taskIdHidden').val() + ' > td.priorityvalue').text());
                    $('input#editTaskProgression').val($('tr#task-' + $('#taskIdHidden').val() + ' > td.progression').text());
                    $('input#editTaskUserName').val($('tr#task-' + $('#taskIdHidden').val() + ' > td.assigne').text());
                    return false;
                });

                // Supprimer une tâche
                $('tr#task-' + myData[i].Id + ' > td.delb > a').click(function () {
                    deleteTask($(this));
                });

                $(".tb").show();
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}


// REALISE UNE FOIS AU DOM READY.
$(document).ready(function () {
    $("body").append('<div id="dialog" title="Popup"></div>');
    $("#dialog").html('<FORM id="taskEditForm" METHOD="POST" ACTION="#" class="jqtransform taskEditForm"> Titre :<br /><INPUT TYPE="text" id=\"editTaskTitle\" SIZE="30"><br /> Description:<br /><INPUT TYPE="text" id=\"editTaskDescription\" SIZE="30"><br />Priorite (LOW - MEDIUM - HIGTH - URGENT):<br /><INPUT TYPE="text" id=\"editTaskPriority\" SIZE="30"><br />Progression (0 - 100):<br /><INPUT TYPE="text" id=\"editTaskProgression\" SIZE="30"><br />Assigne:<br /><INPUT TYPE="text" id=\"editTaskUserName\" SIZE="30"><input type="hidden" id="taskIdHidden"><br /><input class="editb" type="submit" value="Modifier"></form>');
    var $dialog = $('#dialog').dialog({
        autoOpen: false,
        title: "Modification d une tache",
        width: 500,
        height: 300
    });

    $('.taskEditForm').submit(function (myEvent) {
        myEvent.preventDefault();
        $.ajax({
            url: "/Task/Update",
            type: 'PUT',
            dataType: 'json',
            data: {
                'Id': $('#taskIdHidden').val(),
                'Title': $('input#editTaskTitle').val(),
                'Description': $('input#editTaskDescription').val(),
                'PriorityLabel': $('input#editTaskPriority').val(),
                'Progression': $('input#editTaskProgression').val(),
                'UserName': $('input#editTaskUserName').val()
            },
            success: function (myData) {
                TaskRefresh();
                $('#dialog').dialog('close');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("Une erreur s'est produite lors de la modification de la tâche !");
            }
        });
        return false;
    });

    //Ajouter une tâche
    $('.taskAdd').click(function () {
        createTask();
    });
});